<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible"
    width="900px">
    
    <gather-component v-show="showType == 'baseGatherInfo'" ref="addOrUpdategather" @openAddConfigGather="openAddConfigGather" @openAddDecodeInfo="openAddDecodeInfo" @refreshGather="refreshGather"></gather-component>
    <configgather-component v-show="showType == 'configGatherInfo'" ref="addOrUpdateConfigGather" @refreshAddFtpStatus="refreshAddFtpStatus" @addFtpSussess="addFtpSussess" @refreshConfigGatherDataList="refreshConfigGatherDataList"></configgather-component>
    <decode-component v-show="showType == 'decodeInfo'" ref="addOrUpdateDecode" @refreshDecodeData="refreshDecodeData" @disDecodeVisible="disDecodeVisible"></decode-component>

    <span slot="footer" class="dialog-footer" v-if="showType === 'baseGatherInfo'">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="gatherDataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>

    <span slot="footer" class="dialog-footer" v-if="showType === 'configGatherInfo'">
      <el-button @click="visible = false" >取消</el-button>
      <el-button type="primary" @click="backToGather()" v-if="!addFtpStatus">返回</el-button>
      <el-button type="primary" @click="backToConfigGather()" v-if="addFtpStatus">返回</el-button>
      <el-button type="primary" @click="checkFtpStatus()" v-if="canSubmit && addFtpStatus">FTP状态检查</el-button>
      <el-button type="primary" @click="ftpDataFormSubmit()" v-if="canSubmit && addFtpStatus">添加FTP</el-button>
      <el-button type="primary" @click="configGatherSubmit()" v-if="canSubmit && !addFtpStatus">确定</el-button>
    </span>

    <span slot="footer" class="dialog-footer" v-show="showType == 'decodeInfo'">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="backToGather()" v-if="!addFtpStatus">返回</el-button>
      <el-button type="primary" @click="decodeFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>

  </el-dialog>
</template>

<script>

  import {getApplyInfoById} from '@/api/gather/applyinfo'

  import GatherComponent from './gather-component'

  import ConfiggatherComponent from '../../gather/configgather/configgather-component'

  import DecodeComponent from '../../gather/configdecode/configdecode-component'



  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        addFtpStatus: false,
        showType: 'baseGatherInfo',
        addVisible: false,
        decodeList: [],
        gatherList: [],
        applyInfoList: [],
        applyInfo: {},
        dataForm: {
          type: '',
          id: '',
          applyInfoId: '',
          gatherId: undefined,
          decodeId: undefined,
          checkInfoId: '',
          executorBlockStrategy: '',
          executorFailRetryCount: '',
          executorHandler: '',
          executorParam: '',
          executorRouteStrategy: '',
          executorTimeout: '',
          isAlarm: '',
          jobCron: '',
          jobDesc: '',
          jobGroup: '',
          retryInterval: '',
          taskName: '',
          triggerLastTime: '',
          triggerNextTime: '',
          triggerStatus: '',
        },

        dataRule: {
          type: [
            {required: true, message: 'type不能为空', trigger: 'blur'}
          ],
          executorBlockStrategy: [
            {required: true, message: 'executorBlockStrategy不能为空', trigger: 'blur'}
          ],
          executorFailRetryCount: [
            {required: true, message: 'executorFailRetryCount不能为空', trigger: 'blur'}
          ],
          executorHandler: [
            {required: true, message: 'executorHandler不能为空', trigger: 'blur'}
          ],
          executorParam: [
            {required: true, message: 'executorParam不能为空', trigger: 'blur'}
          ],
          executorRouteStrategy: [
            {required: true, message: 'executorRouteStrategy不能为空', trigger: 'blur'}
          ],
          executorTimeout: [
            {required: true, message: 'executorTimeout不能为空', trigger: 'blur'}
          ],
          isAlarm: [
            {required: true, message: 'isAlarm不能为空', trigger: 'blur'}
          ],
          jobCron: [
            {required: true, message: 'jobCron不能为空', trigger: 'blur'}
          ],
          jobDesc: [
            {required: true, message: 'jobDesc不能为空', trigger: 'blur'}
          ],
          jobGroup: [
            {required: true, message: 'jobGroup不能为空', trigger: 'blur'}
          ],
          retryInterval: [
            {required: true, message: 'retryInterval不能为空', trigger: 'blur'}
          ],
          taskName: [
            {required: true, message: 'taskName不能为空', trigger: 'blur'}
          ],
          triggerLastTime: [
            {required: true, message: 'triggerLastTime不能为空', trigger: 'blur'}
          ],
          triggerNextTime: [
            {required: true, message: 'triggerNextTime不能为空', trigger: 'blur'}
          ],
          triggerStatus: [
            {required: true, message: 'triggerStatus不能为空', trigger: 'blur'}
          ],
          gatherId: [
            {required: true, message: '收集配置不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
      ConfiggatherComponent,
      GatherComponent,
      DecodeComponent
    },
    methods: {
      refreshGather () {
        this.visible = false
        this.$emit('refreshDataList')
      },
      gatherDataFormSubmit() {
        this.$refs.addOrUpdategather.dataFormSubmit()
      },
      //打开收集基本配置
      openAddConfigGather () {
        this.showType = 'configGatherInfo'
        this.$refs.addOrUpdateConfigGather.init()
      },
      openAddDecodeInfo () {
        this.showType = 'decodeInfo'
        this.$refs.addOrUpdateDecode.init()
      },
      initByApplyId (applyId) {
        this.init()
        getApplyInfoById(applyId).then(response => {
          if (response.status == 200 && response.data.code == 0) {
            this.dataForm.applyInfoId = applyId
            this.applyInfo = response.data.data
            this.dataForm.decodeId = this.applyInfo.applyDecodeId
            this.gatherDataForm.gatherFtpId = this.applyInfo.applyFtpId
          }
        })
      },
      //收集任务初始化
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.showType = 'baseGatherInfo';
        this.$nextTick(() => {
          this.$refs.addOrUpdategather.init(id)
        })
      },
      //添加ftp成功后，回退到收集基本配置
      addFtpSussess () {
        this.backToConfigGather()
      },
      //
      refreshConfigGatherDataList (configGatherId) {
        this.backToGather()
        this.$refs.addOrUpdategather.setConfigGather(configGatherId)
        // this.$emit('refreshDataList')
      },
      refreshAddFtpStatus (status) {
        this.addFtpStatus = status
      },
      backToGather () {
        this.showType = 'baseGatherInfo'
      },
      backToConfigGather () {
        this.addFtpStatus = false
        this.$refs.addOrUpdateConfigGather.ftpVisible = false
      },
      // init (id) {
      //   this.visible = true;
      //   this.canSubmit = true;
      //   this.dataForm.id = id
      //   this.$nextTick(() => {
      //     this.$refs.addOrUpdateConfigGather.init(id)
      //   })
      // },
      checkFtpStatus () {
        this.$refs.addOrUpdateConfigGather.checkFtpStatus()
      },
      ftpDataFormSubmit () {
        this.$refs.addOrUpdateConfigGather.ftpDataFormSubmit()
      },
      // 表单提交
      configGatherSubmit () {
        this.$refs.addOrUpdateConfigGather.dataFormSubmit()
      },
      decodeFormSubmit () {
        this.$refs.addOrUpdateDecode.dataFormSubmit()
      },
      refreshDecodeData (decodeId) {
        this.$refs.addOrUpdategather.setDecodeId(decodeId)
      },
      disDecodeVisible () {
        this.backToGather()
      },
      //重置表单
      closeDialog() {
        this.$refs.addOrUpdateDecode.closeDialog()
        this.$refs.addOrUpdateConfigGather.closeDialog()
        this.$refs.addOrUpdategather.closeDialog()
      }
    }
  }
</script>
